﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace BI.CF.UI.Models
{
    public class YunPianSmsClient
    {
        private static HttpSessionState _session = HttpContext.Current.Session;

        public static HttpSessionState Session
        {
            get { return YunPianSmsClient._session; }
            set { YunPianSmsClient._session = value; }
        }
        public string SendSms(string mobile)
        {
            // 设置为您的apikey(https://www.yunpian.com)可查
            string apikey = "292e0cf296c5ff7ccff4b425c4694942";
            // 发送的手机号
            //string mobile = "13910386547";
            int tpl_id = 1719498;
            // 发送模板编号
            //if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["YunPianMsnModelId"]))
            //{
            //    tpl_id = Convert.ToInt32(ConfigurationManager.AppSettings["YunPianMsnModelId"]);
            //}
            string PhoneCode = GetPhoneCode() ;
            // 发送模板内容
            mobile = HttpUtility.UrlEncode(mobile, Encoding.UTF8);
            string tpl_value = HttpUtility.UrlEncode(
             HttpUtility.UrlEncode("#code#", Encoding.UTF8) + "=" +
             HttpUtility.UrlEncode(PhoneCode, Encoding.UTF8), Encoding.UTF8);
            //HttpUtility.UrlEncode("云片网", Encoding.UTF8), Encoding.UTF8);
            // 发送内容
            string text = "您的验证码是1234【云片网】";
            // 获取user信息url
            //string url_get_user     = "https://sms.yunpian.com/v1/user/get.json";
            // 智能模板发送短信url
            //string url_send_sms     = "https://sms.yunpian.com/v1/sms/send.json";
            // 指定模板发送短信url
            string url_tpl_sms = "https://sms.yunpian.com/v1/sms/tpl_send.json";
            // 发送语音短信url
            //string url_send_voice = "https://voice.yunpian.com/v1/voice/send.json";
            //string data_get_user = "apikey=" + apikey;
            //string data_send_sms = "apikey=" + apikey + "&mobile=" + mobile + "&text=" + text;
            string data_tpl_sms = "apikey=" + apikey + "&mobile=" + mobile + "&tpl_id=" + tpl_id.ToString() + "&tpl_value=" + tpl_value;
            //string data_send_voice = "apikey=" + apikey + "&mobile=" + mobile + "&code=" + "1234";


            //HttpPost(url_get_user, data_get_user);
            //HttpPost(url_send_sms, data_send_sms);
            string Result = HttpPost(url_tpl_sms, data_tpl_sms);
            return Result;
            //HttpPost(url_send_voice, data_send_voice);
        }

        public static string HttpPost(string Url, string postDataStr)
        {
            byte[] dataArray = Encoding.UTF8.GetBytes(postDataStr);
            // Console.Write(Encoding.UTF8.GetString(dataArray));

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = dataArray.Length;
            //request.CookieContainer = cookie;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                String res = reader.ReadToEnd();
                reader.Close();
                return res;
                //Console.Write("\nResponse Content:\n" + res + "\n");
            }
            catch (Exception e)
            {
                return e.Message + e.ToString();
                //Console.Write(e.Message + e.ToString());
            }
        }
        /// <summary>
        /// 获取手机验证码
        /// </summary>
        /// <returns></returns>
        public string GetPhoneCode()
        {
            Random objDom = new Random();
            StringBuilder BuilderString = new StringBuilder();
            for (int i = 0; i < 10; i++)
            {
                BuilderString.Append(i);
            }
            string PhoneCodeString = "";
            for (int i = 0; i < 6; i++)
            {
                int index = objDom.Next(0, BuilderString.Length);
                PhoneCodeString += index;
                BuilderString.Remove(index, 1);
            }
            Session["PhoneCodeString"] = PhoneCodeString;
            return PhoneCodeString;

        }
    }

}

